<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" 
    import = "java.sql.*" 
    import = "java.util.Date"
    import = "java.text.DateFormat"
    import = "java.text.SimpleDateFormat"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
	Class.forName("com.mysql.jdbc.Driver");
	request.setCharacterEncoding("UTF-8");
	String room_id = request.getParameter("room_id");
	
	String customer_card = request.getParameter("customer_card");
	String customer_name = request.getParameter("customer_name");
	String customer_address = request.getParameter("customer_address");
	
	String indate = request.getParameter("in_date");
	String number = request.getParameter("number");
	String price_1 = request.getParameter("price_1");
	String price_2 = request.getParameter("price_2");
	String price_3 = request.getParameter("price_3");
	
	if (room_id.length()==0) {
		response.sendRedirect("ru_detail.jsp?id="+room_id);
	}
	
// 	System.out.println(service_id+" "+service_name+" "+service_price+" "+service_number);
	Connection con = null;
	Statement smt = null;
	ResultSet rst = null;
	ResultSet rst1 = null;
	String SQL = null;
	try {
		String username = "root";
		String password = "Vietyeutam1112";
		String url = "jdbc:mysql://127.0.0.1:3306/ghmotel?useUnicode=true&characterEncoding=UTF-8";
		con = DriverManager.getConnection(url, username, password);
		System.out.println("Da ket noi CSDL");
		smt = con.createStatement();
		
		
		String SQL3 = "CALL getRoomInfo("+room_id+")";
		
		rst = smt.executeQuery(SQL3);
		
		int price1= 0;
		int price2 = 0;
		int price3=0;
		int money =0;
		
		
		while (rst.next()) {
			 price1= Integer.parseInt(rst.getString(5));
			 price2 = Integer.parseInt(rst.getString(6));
			 price3=Integer.parseInt(rst.getString(7));
			 indate = rst.getString(3);
			 number=rst.getString(4);
			 customer_card = rst.getString(2);
		}
		
		
		Date date = new Date();
		
		String format = "EEE MMM dd kk:mm:ss z yyyy";
		String indateformat = "yyyy-MM-dd hh:mm:ss";

		SimpleDateFormat sdf = new SimpleDateFormat(format);
		SimpleDateFormat sdf2 = new SimpleDateFormat(indateformat);
		
		
		
		Date dateObj1 = (Date) sdf2.parse(indate);
		
		
		
		Date dateObj2 = (Date) sdf.parse(date.toString());
		
		
		
		long diff = dateObj2.getTime() - dateObj1.getTime();
		double diffInHours = diff / ((double) 1000 * 60 * 60);

		int DD = ((int) ((int) diffInHours) / 24);
		int HH = ((int) diffInHours) - DD * 24;
		int MM = (int) ((diffInHours - (int) diffInHours) * 60);


		
		// calc hours money
					int money_h = price3 * HH + ((int) (MM / 30)) * price3;
					if (money_h > price1) {
						money_h = price1;
					}

					// calc money
					if (DD == 0) {
						money = price3 * (HH - 1) + price2;
						if (money > price1) {
							money = price1;
						}
					} else {
						money = price1 * DD + money_h;
					}

					// calc service money
// 					int total = 0;

// 					int rows = tableServiceUsing.getRowCount();

// 					for (int i = 0; i < rows; i++) {
// 						double price = (Double.valueOf((tableServiceUsing.getValueAt(i,
// 								2).toString())));
// 						double number = (Double.valueOf((tableServiceUsing.getValueAt(
// 								i, 3).toString())));

// 						total += price * number;
// 					}

		//Date date = (Date) (spinnerInTime.getValue());
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String out_date = df.format(date);


		SQL = String.format("DELETE FROM room_using WHERE room_id = %s",room_id);
		String Sql2 = String.format("DELETE FROM service_using WHERE room_id = %s",room_id);

		String strSqlInsertHistory = String.format("INSERT INTO history VALUES (%s,%s,%s,'%s','%s',%s,%s,%s,%s,%s)",
				room_id,customer_card,number,indate,out_date,DD,HH,money,0,money+0);
		
// 		out.println(SQL);
// 		out.println(Sql2);
// 		out.println(strSqlInsertHistory);
		
		smt.execute(SQL);
		smt.execute(Sql2);
		smt.execute(strSqlInsertHistory);
		
		
		con.close();
		System.out.println("Dong ket noi");
		//response.sendRedirect("index.jsp");
		out.println("<center> HÓA ĐƠN THANH TOÁN </center></br>");
		
		out.println("<table border = 1 +width=1024 align=center>");
		out.println("<tr><td width=30% align=right >Tên phòng: </td><td width=35% align=left><font color=red>"+ room_id +"</td></tr>");
		out.println("<tr><td width=30% align=right> Chứng minh nhân dân:</td>");
		out.println("<td width=35% align=left>"+ customer_card +"</td></tr>");
		out.println("<tr><td width=30% align=right> Số người:</td>");
		out.println("<td width=35% align=left>"+ number +"</td></tr>");
		out.println("<tr><td width=30% align=right> Giờ vào:</td>");
		out.println("<td width=35% align=left>"+ indate +"</td></tr>");
		out.println("<tr><td width=30% align=right> Giờ ra:</td>");
		out.println("<td width=35% align=left>"+ out_date +"</td></tr>");
		out.println("<tr><td width=30% align=right> Tổng tiền:</td>");
		out.println("<td width=35% align=left><font color=red>"+ money +" VND</td></tr>");
		out.println("<tr><td colspan=2 align=centert ><a href=index.jsp>Trả phòng</a></td></tr>");
		out.println("</table>");
		
	}
	catch (Exception e) {
		e.printStackTrace();
		System.out.println("KHONG KET NOI DUOC");
	}

%>
</body>
</html>

